package com.chenyaowei.web.controller.elasticsearch;

import com.chenyaowei.common.annotation.Anonymous;
import com.chenyaowei.elasticsearch.mapper.ProductMapper;
import com.chenyaowei.elasticsearch.model.Product;
import lombok.RequiredArgsConstructor;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.elasticsearch.index.query.Operator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/es")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProductEeController {

    private final ProductMapper productMapper;

    @GetMapping("/match")
    public EsPageInfo<Product> match(@RequestParam("keyword") String keyword,
                                     @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        // 从多个指定字段中查询包含 keyword 的数据
        LambdaEsQueryWrapper<Product> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.match(Product::getSpuName, keyword)
//                .or().like(Product::getSpuName, keyword)
                .or().like(Product::getBrandName, keyword)
                .or().like(Product::getCatalogName, keyword);

        return productMapper.pageQuery(wrapper, pageNum, pageSize);
    }


}
